﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 2. Write a script that compares two char arrays 
			lexicographically (letter by letter). -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		// Array with all usable ASCII characters
		var charsArr = new Array(127 - 33);
		for (var i = 33, j = 0; i < 127; i++, j++) {
			charsArr[j] = String.fromCharCode(i);
		}

		// Generates a random character
		function randomChar() {
			var randomstring = '';

			var rnum = Math.floor(Math.random() * charsArr.length);
			randomstring = charsArr[rnum];

			return randomstring;
		}

		// Prepare two arrays
		var srcCharArr = new Array(20);
		var destCharArr = new Array(20);

		// Fill them with random characters 
		for (var i = 0; i < 20; i++) {
			srcCharArr[i] = randomChar();
			destCharArr[i] = randomChar();
		}

		// Print two strings
		jsConsole.writeLine("<em>Two random char arrays:</em>");
		jsConsole.writeLine(srcCharArr.join());
		jsConsole.writeLine(destCharArr.join());

		jsConsole.writeLine();

		// Compare two arrays char by char
		jsConsole.writeLine("<em>Lexicographical comparison result:</em>");
		for (var i = 0; i < 20; i++) {
			var result = '';
			if (srcCharArr[i] < destCharArr[i])
			{ result = "less than"; }
			else if (srcCharArr[i] > destCharArr[i])
			{ result = "greater than"; }
			else
			{ result = "equal to"; }
			jsConsole.writeLine("Char " + (i + 1) + " result = [" +
								srcCharArr[i] + "] is " + result +
								" [" + destCharArr[i] + "]");
		}

	</script>
</body>
</html>
